﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EnvDTE80;

namespace SPVisualDev.VSAddin.Dialogs
{
    public partial class DialogDebug : Form
    {
        private List<string[]> m_web_app_list_init = new List<string[]>();
        private SpecialProject m_project;

        public DialogDebug(SpecialProject specialProject)
        {
            InitializeComponent();

            m_project = specialProject;

            this.rdoByPageUrl.CheckedChanged += new EventHandler(rdoByPageUrl_CheckedChanged);
            this.rdoByPageUrl.Checked = true;
            this.rdoByWebApplications.Checked = false;

            this.txtByPage.Enabled = true;
            this.lstWebApplications.Enabled = false;
            this.btnAttach.Enabled = true;

            string s = Common.GetUserRegistryParam("DebugOpenInNewBrowserWindow", true);
            if (!string.IsNullOrEmpty(s))
            {
                this.chkOpenNewBrowser.Checked = bool.Parse(s);
            }
            else
            {
                this.chkOpenNewBrowser.Checked = true;
            }
        }

        private void rdoByPageUrl_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rdoByPageUrl.Checked)
            {
                this.txtByPage.Enabled = true;
                this.lstWebApplications.Enabled = false;
                this.btnPick.Enabled = true;
            }
            else
            {
                this.txtByPage.Enabled = false;
                this.lstWebApplications.Enabled = true;
                this.btnPick.Enabled = false;
            }
        }

        public ListView WebApplicationsListView
        {
            get { return this.lstWebApplications; }
        }

        public string InfoMessage
        {
            get { return this.lblInfo.Text; }
            set { this.lblInfo.Text = value; }
        }

        public List<string[]> WebApplicationsList
        {
            get
            {
                m_web_app_list_init.Clear();

                foreach (ListViewItem item in this.lstWebApplications.Items)
                {
                    m_web_app_list_init.Add(new string[] { item.Text, item.SubItems[1].Text, item.Checked.ToString() });
                }

                return m_web_app_list_init;
            }
            set
            {
                m_web_app_list_init = value;

                this.lstWebApplications.Items.Clear();

                foreach (string[] webapp in m_web_app_list_init)
                {
                    ListViewItem item = new ListViewItem(webapp);

                    if (webapp.Length > 2 && webapp[2] == "True")
                        item.Checked = true;

                    if (webapp.Length > 3 && webapp[3] == "True")
                        item.Selected = true;

                    this.lstWebApplications.Items.Add(item);
                }
            }
        }

        private void btnPick_Click(object sender, EventArgs e)
        {
            try
            {
                using (DialogSharePointExplorer explorer = new DialogSharePointExplorer())
                {
                    explorer.Message = "Choose a site to use as target for debugging.";

                    explorer.SPExplorer.ShowContentTypeGroups = false;
                    explorer.SPExplorer.ShowContentTypes = false;
                    explorer.SPExplorer.ShowFeatures = false;
                    explorer.SPExplorer.ShowFields = false;
                    explorer.SPExplorer.ShowSolutions = false;
                    explorer.SPExplorer.ShowLists = false;

                    explorer.CurrentProject = m_project;

                    DialogResult res = explorer.ShowDialog();
                    if (res != DialogResult.OK)
                        return;

                    if (!string.IsNullOrEmpty(explorer.SelectedUrl))
                        if (explorer.SelectedUrl.ToLower().StartsWith("http://"))
                            this.txtByPage.Text = explorer.SelectedUrl;
                        else
                            MessageBox.Show("No valid location selected.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }

            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }
        }

        private void btnAttach_Click(object sender, EventArgs e)
        {
            try
            {
                Common.SetUserRegistryParam("DebugOpenInNewBrowserWindow", this.chkOpenNewBrowser.Checked.ToString());

                this.DialogResult = DialogResult.OK;
                this.Hide();
            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }

        }
    }

}
